<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2020/10/12
 * Time: 4:06 PM
 */
namespace app\admin\logic;

use app\model\Organization;
use app\model\User;
use app\model\UserOrg;
use app\service\BaiDuService;
use think\facade\Db;
use think\facade\Request;
use tool\PinYin;

class OrgLogic
{

    public function getList()
    {
        $param = Request::get();

        $where = [];

        if(isset($param['orgname']) && !empty($param['orgname'])){
            $where[] = ['orgname','like','%'.trim($param['orgname']).'%'];
        }
        if(config('user.orgid') > 0){
            $where[] = ['id','=',config('user.orgid')];
        }
        $orgModel = new Organization();
        $list = $orgModel->getOrgListByCondition($where);
        return $list;
    }

    public function addOrg()
    {
        $param = Request::post();
        $org = [
            'createid' => config('user')['id'],
            'createname' => config('user')['pername']
        ];
        if(!isset($param['orgname']) || empty($param['orgname']))   fail('组织名称不能为空');
        $org['orgname'] = trim($param['orgname']);

        if(!isset($param['oreaddress']) || empty($param['oreaddress']))   fail('组织地址不能为空');
        $org['oreaddress'] = trim($param['oreaddress']);

        if(!isset($param['ispersonnel']))   fail('请选择是否开启非工作人员扫码');
        $org['ispersonnel'] = intval($param['ispersonnel']);

        if(!isset($param['isphoto']))   fail('请选择是否开启签到上传图片');
        $org['isphoto'] = intval($param['isphoto']);

        if(!isset($param['floor']) || empty($param['floor']))   fail('请填写楼层');
        $org['floor'] = trim($param['floor']);

        $bd = new BaiDuService();
        $geocoding = $bd->getGeocoding($org['oreaddress']);
        if(!$geocoding) fail('请输入准确的地址位置信息');

        $org['lng'] = $geocoding['lng'];
        $org['lat'] = $geocoding['lat'];

        $orgModel = new Organization();
        Db::startTrans();
        try {
            $orgId = $orgModel->addOneOrg($org);
            if(!$orgId){
                Db::rollback();
                fail('组织添加失败');
            }
            //添加组织管理员信息
            $pinYin = new PinYin();
            $user = [
                'position' => User::CLEANING_BOSS,
                'orgid' => $orgId,
                'phone' => $pinYin->getpy($org['orgname'],false),
                'pername' => '管理员',
                'pwd' => encode('cleaning123'),
                'createid' => config('user')['id'],
                'createname' => config('user')['pername'],
            ];
            $userModel = new User();
            $userId = $userModel->addOneUser($user);
            if(!$userId) {
                Db::rollback();
                fail('组织管理员添加失败');
            }
            Db::commit();

        }
        catch (\Exception $e) {
            Db::rollback();
            fail($e);
        }

        return true;

    }
    public function updateOrg()
    {
        $param = Request::post();

        if(!isset($param['orgid']) || empty($param['orgid']))   fail('请选择组织');
        $orgid = intval($param['orgid']);

        $orgModel = new Organization();
        $orgInfo = $orgModel->getOrgInfoById($orgid);
        if(!$orgInfo)   fail('组织不存在');

        $org = [
            'updateid' => config('user')['id'],
            'updatename' => config('user')['pername']
        ];
        if(!isset($param['orgname']) || empty($param['orgname']))   fail('组织名称不能为空');
        $org['orgname'] = trim($param['orgname']);

        if(!isset($param['oreaddress']) || empty($param['oreaddress']))   fail('组织地址不能为空');
        $org['oreaddress'] = trim($param['oreaddress']);

        if(!isset($param['ispersonnel']))   fail('请选择是否开启非工作人员扫码');
        $org['ispersonnel'] = intval($param['ispersonnel']);

        if(!isset($param['isphoto']))   fail('请选择是否开启签到上传图片');
        $org['isphoto'] = intval($param['isphoto']);

        if(!isset($param['floor']) || empty($param['floor']))   fail('请填写楼层');
        $org['floor'] = trim($param['floor']);

        $bd = new BaiDuService();
        $geocoding = $bd->getGeocoding($org['oreaddress']);
        if(!$geocoding) fail('请输入准确的地址位置信息');

        $org['lng'] = $geocoding['lng'];
        $org['lat'] = $geocoding['lat'];
        try {
            $rt = $orgModel->updateOrgInfoByWhere(['id'=>$orgid],$org);
            if(!$rt)    fail('组织信息修改失败');
        }
        catch (\Exception $e) {
            fail($e);
        }

        return true;

    }
    public function close($orgId,$status)
    {
        $orgModel = new Organization();
        $orgInfo = $orgModel->getOrgInfoById($orgId,'orgstatus');

        if(!$orgInfo)   fail('组织信息不存在');

        if($orgInfo['orgstatus'] == $status)    fail('操作失败，相同状态不能修改');

        $org = [
            'orgstatus' => $status,
            'updateid' => config('user')['id'],
            'updatename' => config('user')['pername'],
        ];

        $rt = $orgModel->updateOrgInfoByWhere(['id'=>$orgId],$org);
        if(!$rt)    fail('操作失败，请稍后重试');

        return true;
    }

    public function change($orgid)
    {

        $uid = config('user.id');

        $userOrgModel = new UserOrg();
        //获取当前操作组织
        $info = $userOrgModel->getInfoByUid($uid);

        if($info){
            $userOrgModel->delOrgInfoByUid($uid);
        }

        $userOrg = [
            'uo_uid' => $uid,
            'uo_orgid' => $orgid
        ];
        //添加
        $addRet = $userOrgModel->addOneUserOrg($userOrg);

        if(!$addRet)   fail('操作失败');

        return true;

    }

}